extends Control

const SPRITE_MAX = 16
var _sprite = []
var spriteClass = load("res://scene/play/sprite/sprite.tscn")

func _ready():
# warning-ignore:unused_variable
	for i in range(SPRITE_MAX):
		_sprite.append(null)
	setSlot(0, "sprite")
	pass

func getSlot(index : int):
	if index <= -1 || index >= SPRITE_MAX:
		return null
	return _sprite[index]

func setSlot(index : int, _type : String):
	if index <= -1 || index >= SPRITE_MAX:
		return
	if _type == 'sprite':
		var sp = _sprite[index]
		if sp && sp.type() == "sprite":
			return
		destroySlot(index)
		_sprite[index] = spriteClass.instance()
		add_child(_sprite[index])
		move_child(_sprite[index], index)
		_sprite[index].visible = false
	pass
	
func destroySlot(index : int):
	if _sprite[index] != null:
		remove_child(_sprite[index])
		_sprite[index].queue_free()
		_sprite[index] = null
	pass

func resetAllSlot():
	for i in range(SPRITE_MAX):
		destroySlot(i)
	setSlot(0, "sprite")
	pass
	
func getSaveData():
	var r = []
	for i in range(_sprite.size()):
		var item = _sprite[i]
		if item != null:
			r.append(item.getSaveData())
		else:
			r.append({
				"type": "none"
			})
	return r

func setSaveData(data : Array):
	for i in range(data.size()):
		var item = data[i]
		if item.type == 'none':
			continue
		setSlot(i, item.type)
		_sprite[i].setSaveData(item)
